let tableIns;
let tree;
let startDateIns;
let endDateIns;
layui.use(['element', 'form', 'table', 'layer', 'laydate', 'tree', 'util'], function () {
    let table = layui.table;
    let form = layui.form;//select、单选、复选等依赖form
    let element = layui.element; //导航的hover效果、二级菜单等功能，需要依赖element模块
    let laydate = layui.laydate;
    tree = layui.tree;
    var $=layui.jquery;
    let height = document.documentElement.clientHeight - 60;

    tableIns = table.render({
        elem: '#quarterListTable'
        , url: ctx + '/backend/seller/mobile/projectdetail'
        , method: 'GET'
        //请求前参数处理
        , where: {
                personName: ""
            , sellerName: ""
            , projectName: ""
            , checkedType: ""
            , startDate: ""
            , endDate: ""
        }
        , response: {
            statusName: 'flag' //规定数据状态的字段名称，默认：code
            , statusCode: true //规定成功的状态码，默认：0
            , msgName: 'msg' //规定状态信息的字段名称，默认：msg
        }
        //响应后数据处理
        , parseData: function (res) { //res 即为原始返回的数据
        	var data = res.data;
            var sum=0;
            var taxSum=0;
            var num = 0;
           $.each(data,function(i,obj){
        	   if(obj.status == 'Y'){
        		   sum+=1;
        	   }else if(obj.status == 'T'){
        		   taxSum+=1;
        	   }else{
        		   num+=1;
        	   }
            });
            $("#sum_money1").html("待复核："+sum);
            $("#sum_money2").html("已复核："+taxSum);
            $("#sum_money3").html("已驳回："+num);
            return {
                "flag": res.flag, //解析接口状态
                "msg": res.msg, //解析提示文本
                "data": res.data  //解析数据列表
            };
        }
        ,toolbar:true
        ,title: '发放任务项目承接表'
        ,defaultToolbar: ['filter', 'exports', 'print']
        , cols: [[
                 {field: 'projectCode',width:150,title: '项目编号'}
             ,  {field: 'sellerName',width:150,title: '商户名称'}
            //, {field: 'personName',title: '承接人名称'}
            , {field: 'projectName',title: '项目名称'}
            , {field: 'chargeName',width:100,title: '项目负责人' }
            ,{field: 'telephone',title: '项目负责人电话'}
            , {field: 'projectRequirement',title: '项目描述'}
            , {field: 'createdTime', width:150 ,title: '项目发布时间', templet: function(d){return d.createdTime.substring(0,10);}}
            ,{fixed: 'right', title:'操作', toolbar: '#barBtn', width:200}
           // , {fixed: 'right', width:100, title: '状态', toolbar: '#quarterListTableBar'}
        ]]
       /* , page: true*/
        , height: height
        , cellMinWidth: 80
    });

    var $=layui.jquery;
    $(".time_btn").click(function(){
        $(".time_btn").addClass("layui-btn-radius").addClass("layui-btn-primary");
        $(this).removeClass("layui-btn-radius").removeClass("layui-btn-primary");
        $(".time_btn").removeClass("checked_btn");
        $(this).addClass("checked_btn");
        if($(this).html()=="自定义"){
            $("#queryByStartDate").removeClass("layui-hide");
            $("#queryByEndDate").removeClass("layui-hide");
        }else{
            $("#queryByStartDate").addClass("layui-hide");
            $("#queryByEndDate").addClass("layui-hide");
        }
    });

    $("#queryBtn,.click_btn").click(function(){
        //var queryUserName=$("#queryUserName").val();
        let queryBySellerName = $("#queryBySellerName").val();
        let queryByAgentName = $("#queryByAgentName").val();
        let queryByStartDate = $("#queryByStartDate").val();
        let queryByEndDate = $("#queryByEndDate").val();
        let checked_btn=$(".checked_btn").html();
        let query = {
            done: function (res, curr, count) {
                //完成后重置where，解决下一次请求携带旧数据
                this.where = {};
            }
        };
        //设定异步数据接口的额外参数
        query.where = {
            personName: ""
            , sellerName: queryBySellerName
            , projectName: queryByAgentName
            , checkedType: checked_btn
            , startDate: queryByStartDate
            , endDate: queryByEndDate};
        console.info(query.where);
        tableIns.reload(query);

    })

    
        table.on('tool(display)',function (obj) {
        var _data = obj.data;
        if(obj.event === 'detailed_a'){
            layer.open({
                type: 2,
                title: '人员明细',
                closeBtn: 1, //显示关闭按钮
                anim: 2,
                area: ['800px', '450px'],
                shadeClose: true, //开启遮罩关闭
                content: '/seller/query/dtls?'+_data.projectCode,
                success: function(layero, index){

                }
            });
        }
        
      //复核
        if (obj.event === 'adopt') {
            layer.confirm('确认复核？', function (index) {
            	$.ajax({
                    url: "/seller/query/update?projectCode="+_data.projectCode+"&types=T",
                    dataType:'json',
                    type:'POST',
                    success:function(data){
                    	layer.alert(data.responseText, {icon: 1,closeBtn: 0 },
                    			function(){
                    	window.location.reload();
                    	});
                    },error:function(data){
                    		layer.alert(data.responseText, {icon: 1,closeBtn: 0 },
                        			function(){
                        	window.location.reload();
                        	});
                    }
                })
            });
        }
        //驳回
        else if (obj.event === 'refuse') {
        	layer.confirm('确认驳回？', function (index) {
        	$.ajax({
                url: "/seller/query/update?projectCode="+_data.projectCode+"&types=G",
                dataType:'json',
                type:'POST',
                success:function(data){
                	layer.alert(data.responseText, {icon: 1,closeBtn: 0 },
                			function(){
                	window.location.reload();
                	});
                },error:function(data){
                		layer.alert(data.responseText, {icon: 1,closeBtn: 0 },
                    			function(){
                    	window.location.reload();
                    	});
                }
            })
        	});
            
            
        }

        
    });


    //日期选择器
    startDateIns = laydate.render({
        elem: '#queryByStartDate',
        format: "yyyy-MM-dd",
        trigger: 'click'
    });

    //日期选择器
    endDateIns = laydate.render({
        elem: '#queryByEndDate',
        format: "yyyy-MM-dd",
        trigger: 'click'
    });
});

